package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService service;

    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee emp){
        //1. 将页面提交的密码 password 进行md5加密处理
        String password = emp.getPassword();
        String pwdMd5 = DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));

        //2. 根据页面提交的用户名username查询数据库
        LambdaQueryWrapper<Employee> qw = new LambdaQueryWrapper<>();
        qw.eq(Employee::getUsername,emp.getUsername());
        Employee employee = service.getOne(qw);

        //3. 如果没有查询到则返回登录失败结果
        if (employee == null){
            return R.error("登录失败!");
        }

        //4. 密码比对,如果不一致则返回登录失败结果
        if (!employee.getPassword().equals(pwdMd5)){
            return R.error("登录失败!");
        }

        //5. 查看员工状态, 如果为已禁用状态,则返回员工已禁用结果
        if (employee.getStatus() == 0){
            return R.error("账号已禁用!");
        }
        //6. 登录成功,将员工id存入Session并返回登录成功结果
        request.getSession().setAttribute("employee",employee.getId());

        return R.success(employee);
    }

    @PostMapping("/logout")
    public R<String> logOut(HttpServletRequest request){
        //6. 登录成功,将员工id存入Session并返回登录成功结果
        request.getSession().removeAttribute("employee");

        return R.success("登出成功!");
    }

    @PostMapping
    public R<String> add(@RequestBody Employee emp){
        //1. 设置默认初始密码并进行md5加密处理
        emp.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes(StandardCharsets.UTF_8)));

        //2. 指挥业务层添加
        service.save(emp);

        //3. 返回结果
        return R.success("success");

    }

    @GetMapping("/page")
    public R<Page<Employee>> pageQuery(int page,int pageSize,String name){
        log.info("接收到分页参数:page:{},pageSize:{}",page,pageSize);
        //构建分页构造器
        Page<Employee> pageInfo = new Page<>(page,pageSize);

        //构建条件构造器
        LambdaQueryWrapper<Employee>wrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        wrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //添加排序条件
        wrapper.orderByDesc(Employee::getUpdateTime);

        //执行查询
        service.page(pageInfo,wrapper);

        return R.success(pageInfo);
    }

    @PutMapping
    public R<String> update(@RequestBody Employee emp){
        log.info("执行修改操作,接收到客户端请求数据:{}",emp);
        //1. 指挥业务层修改
        service.updateById(emp);

        //2. 返回结果
        return R.success("success");
    }

    @GetMapping("/{id}")
    public R<Object> findById(@PathVariable String id){
        log.info("执行查看详情操作,接收到客户端请求数据:{}",id);
        //1. 指挥业务层根据id查询
        Employee emp = service.getById(id);

        if (emp != null){
            return R.success(emp);
        }else{
            return R.error("查询失败!用户信息不存在!");
        }

    }

}
